/**
 * 
 */
package util.ui;

import java.awt.*;
import javax.swing.*;

/**
 * @author brian
 *
 */
public class GridBagPanel extends JPanel
{
	public GridBagPanel ()
	{
		constraints = new GridBagConstraints ();
		constraints.insets = defaultInsets;
		setStretchCells ( STRETCH_BOTH );
		
		glueRowConstraints = new GridBagConstraints ();
		glueRowConstraints.weightx = 1;
		glueRowConstraints.weighty = 1;
		glueRowConstraints.fill = GridBagConstraints.BOTH;
		glueRowConstraints.gridwidth = GridBagConstraints.REMAINDER;
		
		resetPanel ();
	}
	
	static public final int STRETCH_NEITHER = 0;
	static public final int STRETCH_VERTICALLY = 1;
	static public final int STRETCH_HORIZONTALLY = 2;
	static public final int STRETCH_BOTH = 3;
		
	public void setStretchCells ( int nType )
	{
		switch ( nType )
		{
			case STRETCH_BOTH:
				constraints.anchor = GridBagConstraints.CENTER;
				constraints.weightx = 1;
				constraints.weighty = 1;
				constraints.fill = GridBagConstraints.BOTH;
				break;
			case STRETCH_HORIZONTALLY:
				constraints.anchor = GridBagConstraints.NORTH;
				constraints.weightx = 1;
				constraints.weighty = 0;
				constraints.fill = GridBagConstraints.HORIZONTAL;
				break;
			case STRETCH_VERTICALLY:
				constraints.anchor = GridBagConstraints.WEST;
				constraints.weightx = 0;
				constraints.weighty = 1;
				constraints.fill = GridBagConstraints.VERTICAL;
				break;
			case STRETCH_NEITHER:
				constraints.anchor = GridBagConstraints.NORTHWEST;
				constraints.weightx = 0;
				constraints.weighty = 0;
				constraints.fill = GridBagConstraints.NONE;
				break;
			default:
				throw new RuntimeException ( "Invalid type of " + nType );
		}
		nStretchType = nType;
	}
	
	public void resetPanel ()
	{
		removeAll();
		layout = new GridBagLayout ();
		setLayout ( layout );		
	}

	public void nextWidgetInRow ( Component toAdd )
	{
		nextWidgetInRow ( toAdd, null );
	}
	
	public void nextWidgetInRow ( Component toAdd, Insets theInset )
	{
		if ( theInset != null )
			constraints.insets = theInset;
		layout.setConstraints( toAdd, constraints );
		add ( toAdd );
		if ( theInset != null )
			constraints.insets = defaultInsets;
	}

	public void lastWidgetInRow ( Component toAdd )
	{
		lastWidgetInRow ( toAdd, null );
	}
	
	public void lastWidgetInRow ( Component toAdd, Insets theInset )
	{
		// Add the input widget
		boolean bNeedGlue = nStretchType == STRETCH_NEITHER || nStretchType == STRETCH_VERTICALLY;
		if ( !bNeedGlue )
			constraints.gridwidth = GridBagConstraints.REMAINDER;
		nextWidgetInRow ( toAdd, theInset );
		
		// If the grid is fixed, add a final "glue" column
		// to absorb the rest of the horizontal space
		if ( bNeedGlue )
		{
			constraints.gridwidth = GridBagConstraints.REMAINDER;
			constraints.weightx = 1;
			constraints.fill = GridBagConstraints.HORIZONTAL;
			nextWidgetInRow ( Box.createHorizontalGlue() );
			constraints.fill = GridBagConstraints.NONE;	
			constraints.weightx = 0;
		}	
		
		constraints.gridwidth = 1;
	}
	
	public void nextGlueRow ()
	{
		Component comp = Box.createVerticalGlue();
		layout.setConstraints( comp, glueRowConstraints );
		add ( comp );	
	}
	
	private int nStretchType = STRETCH_NEITHER;
	private Insets defaultInsets = new Insets ( 5, 5, 5, 5 );
	private GridBagConstraints glueRowConstraints = null;
	private GridBagConstraints constraints = null;
	private GridBagLayout layout = null;
	
    private static final long serialVersionUID = 1;

}
